package com.example.week1;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mylibrary.okhttp.MyCallBack;
import com.example.week1.R;
import com.example.week1.model.LoginModel;
import com.example.week1.model.LoginModelImpl;
import com.example.week1.sql.MySql;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {
    @BindView(R.id.et1)
    EditText et1;
    @BindView(R.id.et2)
    EditText et2;
    @BindView(R.id.bt)
    Button bt;
    String s,s1;
    private LoginModel loginModel;   //okHttp
    private Handler handler=new Handler();
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loginModel=new LoginModelImpl();
        Intent intent = getIntent();
         s = intent.getStringExtra("s");
        s1 = intent.getStringExtra("s1");
        et1.setText(s);
        et2.setText(s1);
        MySql mySql = new MySql(this); //数据库
        db = mySql.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("user",s);
        contentValues.put("pwd",s1);
        db.insert("hai",null,contentValues);
    }



    @OnClick({R.id.et1, R.id.et2, R.id.bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et1:
                break;
            case R.id.et2:
                break;
            case R.id.bt:
                loginModel.login(s, s1, new MyCallBack() {
                    @Override
                    public void OnOk(String str) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this, ""+str, Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(LoginActivity.this,MainActivity.class));
                            }
                        });
                    }

                    @Override
                    public void OnNo(String message) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this, ""+message, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
//                initData();
//                startActivity(new Intent(LoginActivity.this,MainActivity.class));
//
                break;
        }
    }

    private void initData() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("username",s);
        hashMap.put("password",s1);
        OkGo.<String>post("https://www.wanandroid.com/user/login")
                .params(hashMap)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                    }
                });

    }

}
